﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using Admin;
using EMPower.Utility;

public partial class PersonalInfoManage_RegionAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SubmitButton.Visible = true;
            this.UpdateButton.Visible = false;

            string famousRegionID = Request.QueryString["UID"];
            if (!string.IsNullOrWhiteSpace(famousRegionID))
            {
                FamousRegionInfo famousRegionInfo = BLL.BFamousRegion.getInstance().GetFamousRegionInfo(Convert.ToInt64(famousRegionID));
                if (famousRegionInfo != null)
                {
                    this.regionTitle.Text = famousRegionInfo.Title;
                    this.regionUserName.Text = famousRegionInfo.Name;
                    this.regionUserAddr.Text = famousRegionInfo.Address;
                    this.famousRegionID.Value = famousRegionID;
                    this.regionTitle.ReadOnly = true;
                    this.SubmitButton.Visible = false;
                    this.UpdateButton.Visible = true;
                    this.updateflg.Value = "1";
                }
            }
            BindFamousRegion();
        }
    }

    private void BindFamousRegion()
    {
        this.FamousRegionDataGrid.DataSource = BLL.BFamousRegion.getInstance().GetAllFamousRegions();
        this.FamousRegionDataGrid.DataBind();
    }
    // 提交
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        string title = this.regionTitle.Text;
        string regionUserName = this.regionUserName.Text;
        string regionAddr = this.regionUserAddr.Text;
        string imageSrc = "";

        if (this.file != null && !string.IsNullOrEmpty(this.file.FileName))
        {
            //上传图片
            int filelenth = file.PostedFile.ContentLength;
            int fileSize = Convert.ToInt32((filelenth) / 1024);
            if (fileSize > 5000)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('图片大小不超过5M');</script>");
                return;
            }
            string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."));
            string strFileName = IdHelper.GetLongId().ToString() + "_" + DateTime.Now.Millisecond.ToString() + fileType;
            string strNewFileName = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Upload) + strFileName;
            file.SaveAs(strNewFileName);
            //userInfo.IDCardImg = strFileName;
            imageSrc = strFileName;
        }

        // 添加FamousRegion表
        FamousRegionInfo famousRegionInfo = new FamousRegionInfo();
        long famousRegionId = IdHelper.GetLongId();
        famousRegionInfo.Title = title;
        famousRegionInfo.Name = regionUserName;
        famousRegionInfo.Address = regionAddr;
        famousRegionInfo.ImageSrc = imageSrc;
        famousRegionInfo.FamousRegionId = famousRegionId;
        famousRegionInfo.LastUpdate = DateTime.Now;
        famousRegionInfo.DateCreated = DateTime.Now;
        BLL.BFamousRegion.getInstance().Insert(famousRegionInfo);
        // 添加RegionCategory表
        RegionCategoryInfo regionCategoryInfo = new RegionCategoryInfo();
        regionCategoryInfo.RegionCategoryId = IdHelper.GetLongId();
        regionCategoryInfo.FamousRegionId = famousRegionId;
        regionCategoryInfo.CategoryType = 1;
        regionCategoryInfo.Name = title + "-个人动态";
        famousRegionInfo.LastUpdate = DateTime.Now;
        famousRegionInfo.DateCreated = DateTime.Now;
        BLL.BRegionCategory.getInstance().Insert(regionCategoryInfo);
        regionCategoryInfo.RegionCategoryId = IdHelper.GetLongId();
        regionCategoryInfo.FamousRegionId = famousRegionId;
        regionCategoryInfo.CategoryType = 2;
        regionCategoryInfo.Name = title + "-个人事迹";
        famousRegionInfo.LastUpdate = DateTime.Now;
        famousRegionInfo.DateCreated = DateTime.Now;
        BLL.BRegionCategory.getInstance().Insert(regionCategoryInfo);
        regionCategoryInfo.RegionCategoryId = IdHelper.GetLongId();
        regionCategoryInfo.FamousRegionId = famousRegionId;
        regionCategoryInfo.CategoryType = 3;
        regionCategoryInfo.Name = title + "-个人简介";
        famousRegionInfo.LastUpdate = DateTime.Now;
        famousRegionInfo.DateCreated = DateTime.Now;
        BLL.BRegionCategory.getInstance().Insert(regionCategoryInfo);

        BindFamousRegion();
        Response.Redirect("RegionAdd.aspx");
    }
    // 更新
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        string famousRegionID = this.famousRegionID.Value;
        FamousRegionInfo famousRegionInfo = BLL.BFamousRegion.getInstance().GetFamousRegionInfo(Convert.ToInt64(famousRegionID));
        famousRegionInfo.Name = this.regionUserName.Text;
        famousRegionInfo.Address = this.regionUserAddr.Text;
        if (this.file != null && !string.IsNullOrEmpty(this.file.FileName))
        {
            //上传图片
            int filelenth = file.PostedFile.ContentLength;
            int fileSize = Convert.ToInt32((filelenth) / 1024);
            if (fileSize > 5000)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('图片大小不超过5M');</script>");
                return;
            }
            string fileType = file.FileName.Substring(file.FileName.LastIndexOf("."));
            string strFileName = IdHelper.GetLongId().ToString() + "_" + DateTime.Now.Millisecond.ToString() + fileType;
            string strNewFileName = BLL.Common.UploadResource.GetPath(BLL.Common.BizResourceType.UserResource, BLL.Common.BizResourceAccess.Upload) + strFileName;
            file.SaveAs(strNewFileName);
            //userInfo.IDCardImg = strFileName;
            famousRegionInfo.ImageSrc = strFileName;
        }
        famousRegionInfo.Bak1 = null;
        famousRegionInfo.LastUpdate = DateTime.Now;
        BLL.BFamousRegion.getInstance().Update(famousRegionInfo);
        BindFamousRegion();
        Response.Redirect("RegionAdd.aspx");
    }

    protected void FamousRegionDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        this.FamousRegionDataGrid.CurrentPageIndex = e.NewPageIndex;
        BindFamousRegion();
    }
    protected void FamousRegionDataGrid_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
        long famousRegionID = Convert.ToInt64(e.Item.Cells[0].Text);
        FamousRegionInfo famousRegionInfo = BLL.BFamousRegion.getInstance().GetFamousRegionInfo(famousRegionID);        
        famousRegionInfo.Bak1 = "1";
        BLL.BFamousRegion.getInstance().Update(famousRegionInfo);
        BLL.BRegionCategory.getInstance().DeleteByFamousRegionId(famousRegionID);
        BindFamousRegion();
        Init();
    }

    private void Init()
    {
        this.SubmitButton.Visible = true;
        this.UpdateButton.Visible = false;
    }
}